{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "991c884f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Init Plugin\n",
      "WARNING:tensorflow:From /var/folders/qc/87_8h1gj6c540g1c_lmq9t_w0000gn/T/ipykernel_936/3326022288.py:2: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use `tf.config.list_physical_devices('GPU')` instead.\n",
      "Init Graph Optimizer\n",
      "Init Kernel\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-07-31 10:52:06.922720: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.\n",
      "2021-07-31 10:52:06.923155: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Metal device set to: Apple M1\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "tf.test.is_gpu_available()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c7ad0242",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4ffa2c99",
   "metadata": {},
   "outputs": [],
   "source": [
    "fashion_mnist=tf.keras.datasets.fashion_mnist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3c47f94d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz\n",
      "32768/29515 [=================================] - 0s 4us/step\n",
      "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz\n",
      "26427392/26421880 [==============================] - 10s 0us/step\n",
      "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz\n",
      "8192/5148 [===============================================] - 0s 0us/step\n",
      "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz\n",
      "4423680/4422102 [==============================] - 2s 0us/step\n"
     ]
    }
   ],
   "source": [
    "(train_images,train_labels),(test_images,test_labels)=fashion_mnist.load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f7b9e198",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(60000, 28, 28)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_images.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ba48cb35",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(60000,)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_labels.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1d5f0c8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([9, 2, 1, ..., 8, 1, 5], dtype=uint8)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d3327c46",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-07-31 11:04:59.148681: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.\n",
      "2021-07-31 11:04:59.148737: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tf.Tensor: shape=(60000, 10), dtype=float32, numpy=\n",
       "array([[0., 0., 0., ..., 0., 0., 1.],\n",
       "       [1., 0., 0., ..., 0., 0., 0.],\n",
       "       [1., 0., 0., ..., 0., 0., 0.],\n",
       "       ...,\n",
       "       [0., 0., 0., ..., 0., 0., 0.],\n",
       "       [1., 0., 0., ..., 0., 0., 0.],\n",
       "       [0., 0., 0., ..., 0., 0., 0.]], dtype=float32)>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.one_hot(train_labels,10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b1e4497",
   "metadata": {},
   "source": [
    "或者"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9c7bb75f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., ..., 0., 0., 1.],\n",
       "       [1., 0., 0., ..., 0., 0., 0.],\n",
       "       [1., 0., 0., ..., 0., 0., 0.],\n",
       "       ...,\n",
       "       [0., 0., 0., ..., 0., 0., 0.],\n",
       "       [1., 0., 0., ..., 0., 0., 0.],\n",
       "       [0., 0., 0., ..., 0., 0., 0.]], dtype=float32)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf.keras.utils.to_categorical(train_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ddad898d",
   "metadata": {},
   "outputs": [],
   "source": [
    "one_hot_train_labels=tf.keras.utils.to_categorical(train_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "52834eec",
   "metadata": {},
   "outputs": [],
   "source": [
    "one_hot_test_labels=tf.keras.utils.to_categorical(test_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a47d6021",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_images=np.expand_dims(train_images,-1)#扩张纬度\n",
    "test_images=np.expand_dims(test_images,-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "30da53af",
   "metadata": {},
   "outputs": [],
   "source": [
    "model=tf.keras.models.Sequential()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "28af12ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.add(tf.keras.layers.Conv2D(32,(3,3),input_shape=(28,28,1),activation='relu',padding='same'))#或者train_images.shape(:-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "a1a90268",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.add(tf.keras.layers.MaxPool2D())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "ef418097",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.add(tf.keras.layers.Conv2D(64,(3,3),activation='relu',padding='same'))#或者train_images.shape(:-1),input_shape 可以不写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "4c5adea9",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.add(tf.keras.layers.GlobalAvgPool2D())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "5a6d4e74",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.add(tf.keras.layers.Dense(10,activation='softmax'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b15ef3ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential_3\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "conv2d_5 (Conv2D)            (None, 28, 28, 32)        320       \n",
      "_________________________________________________________________\n",
      "max_pooling2d_2 (MaxPooling2 (None, 14, 14, 32)        0         \n",
      "_________________________________________________________________\n",
      "conv2d_6 (Conv2D)            (None, 14, 14, 64)        18496     \n",
      "_________________________________________________________________\n",
      "global_average_pooling2d_2 ( (None, 64)                0         \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              (None, 10)                650       \n",
      "=================================================================\n",
      "Total params: 19,466\n",
      "Trainable params: 19,466\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "e04b5bdc",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.compile(optimizer='adam',\n",
    "              loss='categorical_crossentropy',\n",
    "              metrics=['acc']\n",
    "             )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "cd33ba76",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/12\n",
      "1875/1875 [==============================] - 17s 9ms/step - loss: 0.2547 - acc: 0.9099 - val_loss: 0.3158 - val_acc: 0.8899\n",
      "Epoch 2/12\n",
      "1875/1875 [==============================] - 17s 9ms/step - loss: 0.2506 - acc: 0.9119 - val_loss: 0.2987 - val_acc: 0.8971\n",
      "Epoch 3/12\n",
      "1875/1875 [==============================] - 17s 9ms/step - loss: 0.2470 - acc: 0.9130 - val_loss: 0.3028 - val_acc: 0.8938\n",
      "Epoch 4/12\n",
      "1875/1875 [==============================] - 17s 9ms/step - loss: 0.2412 - acc: 0.9149 - val_loss: 0.3059 - val_acc: 0.8942\n",
      "Epoch 5/12\n",
      "1875/1875 [==============================] - 17s 9ms/step - loss: 0.2394 - acc: 0.9152 - val_loss: 0.3021 - val_acc: 0.8948\n",
      "Epoch 6/12\n",
      "1875/1875 [==============================] - 17s 9ms/step - loss: 0.2343 - acc: 0.9168 - val_loss: 0.2998 - val_acc: 0.8969\n",
      "Epoch 7/12\n",
      "1875/1875 [==============================] - 17s 9ms/step - loss: 0.2300 - acc: 0.9179 - val_loss: 0.3188 - val_acc: 0.8901\n",
      "Epoch 8/12\n",
      "1875/1875 [==============================] - 17s 9ms/step - loss: 0.2293 - acc: 0.9183 - val_loss: 0.3458 - val_acc: 0.8808\n",
      "Epoch 9/12\n",
      "1875/1875 [==============================] - 17s 9ms/step - loss: 0.2250 - acc: 0.9194 - val_loss: 0.3265 - val_acc: 0.8875\n",
      "Epoch 10/12\n",
      "1875/1875 [==============================] - 17s 9ms/step - loss: 0.2200 - acc: 0.9210 - val_loss: 0.3034 - val_acc: 0.8961\n",
      "Epoch 11/12\n",
      "1875/1875 [==============================] - 17s 9ms/step - loss: 0.2181 - acc: 0.9219 - val_loss: 0.2998 - val_acc: 0.8968\n",
      "Epoch 12/12\n",
      "1875/1875 [==============================] - 17s 9ms/step - loss: 0.2136 - acc: 0.9241 - val_loss: 0.3060 - val_acc: 0.8941\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x2aab09220>"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(train_images,one_hot_train_labels,epochs=12,validation_data=(test_images,one_hot_test_labels))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8280d4a3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
